﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OnlineShop
{
    /// <summary>
    /// Interaction logic for AddUser.xaml
    /// </summary>
    public partial class AddUser : Window
    {
        public AddUser()
        {
            InitializeComponent();
        }

        public void OnBackToStartMenuClick(object sender, RoutedEventArgs e)
        {
            StartMenu window = new StartMenu();
            window.Show();
            this.Close();
            
        }

        private void OnAddUserClick(object sender, RoutedEventArgs e)
        {

            this.name.Text = this.name.Text.Trim();
            this.balance.Text = this.balance.Text.Trim();
            User currUser = new User();
            try
            {
                currUser.Name = this.name.Text;
                currUser.Password = this.password.Password;
                currUser.Balance = decimal.Parse(this.balance.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("balance must be number");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            if (File.Exists("Users\\" + currUser.Name + ".txt"))
            {
                MessageBox.Show(string.Format("User {0} already exists", currUser.Name));
                return;
            }
            StreamWriter writer = new StreamWriter("Users\\" + currUser.Name + ".txt");

            using (writer)
            {
                writer.WriteLine("Name:{0}:", currUser.Name);
                writer.WriteLine("Password:{0}:", currUser.Password);
                writer.WriteLine("Balance:{0}:", currUser.Balance);
                MessageBox.Show("Registered succesfully");
            }

        }
    }
}
